Import cal3d

Class CalCoreModel

	Field m_name:String
	Field m_coreSkeleton:CalCoreSkeleton
	Field m_vectorCoreAnimation:CalCoreAnimation[]
	Field m_vectorCoreMorphAnimation:CalCoreMorphAnimation[]
	Field m_vectorCoreMesh:CalCoreMesh[]
	Field m_vectorCoreMaterial:CalCoreMaterial[]
	Field m_mapmapCoreMaterialThread:IntMap<IntMap<Int>>
	Field m_animationName:StringMap<Int>
	Field m_materialName:StringMap<Int>
	Field m_meshName:StringMap<Int>
	Field m_userData:Object
	
	
	Method New(name:String="Model")

		m_name = name	
		m_coreSkeleton = Null
		m_vectorCoreAnimation = New CalCoreAnimation[0]
		m_vectorCoreMorphAnimation = New CalCoreMorphAnimation[0]
		m_vectorCoreMesh = New CalCoreMesh[0]
		m_vectorCoreMaterial = New CalCoreMaterial[0]
		m_mapmapCoreMaterialThread = New IntMap<IntMap<Int>>
		m_animationName = New StringMap<Int>
		m_materialName = New StringMap<Int>
		m_meshName = New StringMap<Int>
		
		
	End
	
	Method getUserData:Object()
		Return m_userData
	End
	
	Method setUserData:Void(userData:Object)
		m_userData = userData
	End
	
	Method scale:Void(factor:Float)
		m_coreSkeleton.scale(factor)
	
		For Local animationId:Int = 0 To m_vectorCoreAnimation.Length-1
			m_vectorCoreAnimation[animationId].scale(factor)
		Next
	
		For Local meshId:Int = 0 To m_vectorCoreMesh.Length-1
			m_vectorCoreMesh[meshId].scale(factor)
		Next	
	
	End
	
	Method addCoreAnimation:Int(coreAnimation:CalCoreAnimation)
		
		local animationId = m_vectorCoreAnimation.Length
		m_vectorCoreAnimation = m_vectorCoreAnimation.Resize(animationId+1) 'push(coreAnimation)
		m_vectorCoreAnimation[animationId] = coreAnimation
		return animationId		
	
	End
	
	Method getCoreAnimation:CalCoreAnimation(coreAnimationId:Int)
	
		If coreAnimationId < 0 Or coreAnimationId >= m_vectorCoreAnimation.Length
			Print "Invalid Animation ID"
			Return Null
		Endif
	
		return m_vectorCoreAnimation[coreAnimationId]		
	
	End

	Method getCoreAnimationCount:Int()
		Return m_vectorCoreAnimation.Length
	End
	
	Method loadCoreAnimation:Int(filename:String)
		Print "Not Implemented! use Loader Class"
		Return -1
	End
	
	Method unloadCoreAnimation:Int(coreAnimationId:Int)
	
		If coreAnimationId < 0 Or coreAnimationId >= m_vectorCoreAnimation.Length
			Print "Invalid Animation ID"
			return -1;
		Endif
	
		m_vectorCoreAnimation[coreAnimationId] = null
		return coreAnimationId
	
	End

	Method unloadCoreAnimation:Int(coreAnimationId:String)
		Local aid:Int = getCoreAnimationId(coreAnimationId)
		If aid < 0 Or aid >= m_vectorCoreAnimation.Length
			Print "Invalid Animation ID"
			return -1;
		Endif
	
		m_vectorCoreAnimation[aid] = null
		return aid
	
	End


	Method saveCoreAnimation:Bool(filename:String, coreAnimationId:Int)
		Print "Not Implemented! use Saver Class"
	End
	

	Method addAnimationName:Bool(name:String, coreAnimationId:Int)
	
		If coreAnimationId < 0 or coreAnimationId >= m_vectorCoreAnimation.Length
			Print "Invalid Animation ID"
			Return False
		Endif
	
		m_vectorCoreAnimation[coreAnimationId].setName(name)
		m_animationName.Set(name,coreAnimationId)
		Return True
		
	End
	
	Method getCoreAnimationId:Int(animationName:String)
	
		If m_animationName.Contains(animationName) = True Then
			Return m_animationName.Get(animationName)
		Else
			Return -1
		Endif
	
	End
	
	Method addCoreMorphAnimation:Int(coreMorphAnimation:CalCoreMorphAnimation)
	
		Local morphAnimationId:int = m_vectorCoreMorphAnimation.Length
		m_vectorCoreMorphAnimation = m_vectorCoreMorphAnimation.Resize(morphAnimationId+1)'push(coreMorphAnimation)
		m_vectorCoreMorphAnimation[morphAnimationId] = coreMorphAnimation
		return morphAnimationId		
	
	End
	
	Method getCoreMorphAnimation:CalCoreMorphAnimation(coreMorphAnimationId:Int)
	
		If coreMorphAnimationId < 0 or coreMorphAnimationId >= m_vectorCoreMorphAnimation.Length
			Print "Invalid Morph ID"
			Return Null
		Endif
	
		Return m_vectorCoreMorphAnimation[coreMorphAnimationId]
		
	End
	
	Method getCoreMorphAnimationCount:Int()
		Return m_vectorCoreMorphAnimation.Length
	End
	
	Method addCoreMaterial:Int(coreMaterial:CalCoreMaterial)
	
		local materialId = m_vectorCoreMaterial.Length
		m_vectorCoreMaterial = m_vectorCoreMaterial.Resize(materialId+1)'push(coreMaterial)
		m_vectorCoreMaterial[materialId] = coreMaterial
		return materialId		
	
	End
	
	Method createCoreMaterialThread:Bool(coreMaterialThreadId:Int)
		'Make sure this is how this is used...
		m_mapmapCoreMaterialThread.Set(coreMaterialThreadId,New IntMap<Int>)
		Return true
	End
	
	Method getCoreMaterial:CalCoreMaterial(coreMaterialId:Int)
		If coreMaterialId < 0 Or coreMaterialId >= m_vectorCoreMaterial.Length then
			Print "Invalid Material ID"
			Return Null
		Endif
	
		return m_vectorCoreMaterial[coreMaterialId]	
	End
	
	Method getCoreMaterialCount:Int()
		Return m_vectorCoreMaterial.Length
	End

	Method getCoreMaterialId:Int(materialName:String)
	
		If m_materialName.Contains(materialName) = True Then
			Return m_materialName.Get(materialName)
		Else
			Return -1
		Endif
	
	End

	Method getCoreMaterialId:Int(materialId:Int,coreMaterialSetId:int)
	
		If m_mapmapCoreMaterialThread.Contains(materialId) = True Then
			Local newmap:IntMap<Int> = m_mapmapCoreMaterialThread.Get(materialId)
			If newmap.Contains(coreMaterialSetId) = True Then
				Return newmap.Get(coreMaterialSetId)
			Else
				Return -1
			Endif
		Else
			Return -1
		Endif
		
	End

	Method loadCoreMaterial:Int(filename:String)
		Print "Not implmented use Loader Class!"
		Return -1
	End
	
	Method unloadCoreMaterial:Int(coreMaterialId:Int)
	
		If coreMaterialId < 0 or coreMaterialId >= m_vectorCoreMaterial.Length
			Print "Invalid Material ID"
			Return -1
		Endif
	
		m_vectorCoreMaterial[coreMaterialId] = null
		return coreMaterialId	
	
	End
	

	Method unloadCoreMaterial:Int(coreMaterialName:String)
	
		Local coreMaterialId:Int = getCoreMaterialId(coreMaterialName)
	
		If coreMaterialId < 0 or coreMaterialId >= m_vectorCoreMaterial.Length
			Print "Invalid Material ID"
			Return -1
		Endif
	
		m_vectorCoreMaterial[coreMaterialId] = null
		return coreMaterialId	
	
	End	
	
	Method saveCoreMaterial:Int(filename:String, coreMaterialId:Int)
		Print "Not Implemented, Use Saver Class!"
		Return -1
	End
	
	Method setCoreMaterialId:Bool(coreMaterialThreadId:Int, coreMaterialSetId:Int, coreMaterialId:Int)
	
		If m_mapmapCoreMaterialThread.Contains(coreMaterialThreadId) = True Then
			Local coreMaterialThread:IntMap<Int> = m_mapmapCoreMaterialThread.Get(coreMaterialThreadId)
			Return coreMaterialThread.Set(coreMaterialSetId,coreMaterialId)
		Else
			Return False
		Endif
	
	End
	
	Method addMaterialName:Bool(name:String, coreMaterialId:Int)
	
	
		If coreMaterialId < 0 Or coreMaterialId >= m_vectorCoreMaterial.Length then
			Print "Invalid Material ID"
			Return False
		Endif
	
		m_vectorCoreMaterial[coreMaterialId].setName(name)
		m_materialName.Set(name,coreMaterialId)
		Return True		
	
	End
	
	
	Method addCoreMesh:Int(coreMesh:CalCoreMesh)
	
		Local meshId:int = m_vectorCoreMesh.Length
		m_vectorCoreMesh = m_vectorCoreMesh.Resize(meshId+1)'push(coreMesh)
		m_vectorCoreMesh[meshId] = coreMesh
		return meshId		
	
	End
	
	Method getCoreMesh:CalCoreMesh(coreMeshId:Int)
	
		If coreMeshId < 0 Or coreMeshId >= m_vectorCoreMesh.Length Then
			Print "Invalid Mesh ID"
			Return Null
		Endif
	
		return m_vectorCoreMesh[coreMeshId]
	
	End
	
	Method getCoreMeshCount:Int()
		
		Return m_vectorCoreMesh.Length
	End
	
	Method loadCoreMesh:Int(filename:String)
		Print "Not Implemented, please use Loader CLass"
		Return -1
	End
	
	Method unloadCoreMesh:Int(coreMeshId:Int)

	
		If coreMeshId < 0 Or coreMeshId >= m_vectorCoreMesh.Length then
			Print "Invalid Mesh ID"
			return -1
		Endif
	
		m_vectorCoreMesh[coreMeshId] = Null
		Return coreMeshId	
	End
	
	Method unloadCoreMesh:Int(coreMeshName:String)
		
		Local coreMeshId:Int = getCoreMeshId(coreMeshName)
	
		If coreMeshId < 0 Or coreMeshId >= m_vectorCoreMesh.Length then
			Print "Invalid Mesh ID"
			return -1
		Endif
	
		m_vectorCoreMesh[coreMeshId] = Null
		Return coreMeshId	
	End
	
		
	Method saveCoreMesh:Int(filename:String, coreMeshId:Int)
		Print "Not Implemented, Use Saver Class"
		Return -1
	End


	Method addMeshName:Bool(meshName:String, coreMeshId:Int)
	
		If coreMeshId < 0 Or coreMeshId >= m_vectorCoreMesh.Length then
			Print "Invalid Mesh ID"
			Return False
		Endif
	
		m_vectorCoreMesh[coreMeshId].setName(meshName)
		m_meshName.Set(meshName,coreMeshId)
		Return True		
	
	End
	
	Method getCoreMeshId:Int(meshName:String)
	
		If m_meshName.Contains(meshName) = True Then
			Return m_meshName.Get(meshName)
		Else
			Return -1
		Endif

	End
	
	Method makemodel:CalModel()'Don't really need this since native code was ported to monkey
	
		Return New CalModel(Self)

	End
	
	
	Method getCoreSkeleton:CalCoreSkeleton()
		Return m_coreSkeleton
	End
	
	Method loadCoreSkeleton:Int(filename:String)
		Print "Not Implemented use Loader Class"
		Return -1
	End
	
	Method saveCoreSkeleton:Int(filename:String)
		Print "Not Implemented use Saver Class"
	End
	
	Method setCoreSkeleton:Void(coreSkeleton:CalCoreSkeleton)
		If coreSkeleton <> Null then
			m_coreSkeleton = coreSkeleton
		Else
			Print "Cannot set NULL Skeleton!"
		Endif
	End
	
	Method addBoneName:Void(boneName:String, boneId:Int)
	
		If m_coreSkeleton <> Null Then
		
			m_coreSkeleton.mapCoreBoneName(boneId, boneName)
		
		Endif
	
	End
	
	Method getBoneId:Int(boneName:String)
	
		If m_coreSkeleton <> Null Then
			m_coreSkeleton.getCoreBoneId(boneName)
		Else
			Return -1
		Endif
	
	End

End Class